package plug.bookmarks;

import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.tree.DefaultMutableTreeNode;

public class Control {
	BeanCL beanCl;
	BookmarksTree bookmarks;// UI

	ArrayList<DefaultMutableTreeNode> group = new ArrayList<DefaultMutableTreeNode>();// 保存所有的分组
	DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");// 根节点
	public BookmarksTree getBookmarks() {
		return bookmarks;
	}
	public Control() {
		// TODO Auto-generated constructor stub
		beanCl = new BeanCL();
		ArrayList<String> titles = beanCl.selectSubgroup();
		DefaultMutableTreeNode groupNode;
		for (String string : titles) {
			groupNode = new DefaultMutableTreeNode(string);
			group.add(groupNode);

			for (DefaultMutableTreeNode nodeg : group) {
				ArrayList array = beanCl.selectBookmarks(nodeg.toString());// 查询得到组的所有的列表
				if (array != null) {
					Bean bean;
					for (int i = 0; i < array.size(); i++) {
						bean = (Bean) array.get(i);
						print("URL:" + bean.url + "\t\tTitle:" + bean.title);

						JLabel lab = new JLabel();
						/*
						 * if(bean.icon!=null)//设置图标 lab.setIcon(bean.icon);
						 */

						lab.setText(bean.title);
						lab.setName(bean.url);// 设置控件的名字为为监听者准备
						// lab.setToolTipText(name);// 显示提示信息
						DefaultMutableTreeNode node = new DefaultMutableTreeNode(
								lab);
						nodeg.add(node);
					}
				} else {
					print("无法获得组下的数据");
				}

				root.add(nodeg);
			}

		}
		titles.clear();
		titles = null;

		bookmarks = new BookmarksTree(root);
		bookmarks.setControl(this);
	}

	private void print(String str) {
		System.out.println("Control类\t" + str);
	}

	public void addBookmark(String url, String title, String subgroup) {
		beanCl.addBookmark(url, title, subgroup);
	}

	public boolean delBookmark(String url) {
		// TODO Auto-generated method stub
		if(
		beanCl.delBookmark(url)){
			return true;
		}else{
			return false;
		}
		
	}
	public static void main(String[] args) {
		JFrame jf = new JFrame("MiniBox");

		Control con = new Control();

		jf.add(con.bookmarks);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设定窗体关闭后自动退出进程
		jf.setSize(200, 500);
		jf.setVisible(true);

	}

}
